import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;

import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

public class ThunderDragonClientHandler implements Runnable {

	private Socket socket;
	public ThunderDragonClientHandler(){
		try{
		socket = new Socket("localhost", 12345);
		}
		catch(UnknownHostException e){
			System.out.println("Host is unknown");
		}
		catch(IOException e){
			System.out.println("Something's wrong with the input/output!");
		}
	}
	public ThunderDragonClientHandler(Socket sock){
		socket = sock;
	}
	
	//THe only thing being transferred: Database.d
	public void run(){
		 //BufferedReader  inStream;
	      //  PrintWriter outStream;
	        ObjectInputStream inputStream;
			ObjectOutputStream outputStream;
	        try{
	        	inputStream = new ObjectInputStream(socket.getInputStream());
	        outputStream = new ObjectOutputStream(socket.getOutputStream());
	        //outputStream.println("Connection Successful.  Client is in Socket.");
	        outputStream.flush();
	        //While it's all true, then do this:
            while(true){
            	try{

                	Object itemFromDatabase = inputStream.readObject();
            	}
            	catch(ClassNotFoundException e){
            		
            	}
            	//Our very last orders.
            	inputStream.close();
            	outputStream.close();
            	socket.close();
            }
	        }
	        catch (SocketException e) {
        System.err.println("Detected Client Disconnect");
	        } catch (IOException e) {
        System.err.println(
            "Error in client handler, could not establish streams or "
             +  " there was a disconnect");
	}
	
}
	
	public String toString(){
		return "ClientHandler";
	}
}

/*   System.out.println("Connection from "
       + mySocket.getInetAddress().getHostName());

   boolean notDone = true;

   try {
       //note that we are using a text protocol since
       //are reading with a buffered reader (1 string line at a time)
       inStream = new BufferedReader(new InputStreamReader(
                   mySocket.getInputStream()));
       outStream = new PrintWriter(mySocket.getOutputStream());
       outStream.println("Connection Successful, Enter Command:");
       outStream.flush();
       //we loop here 
       while (notDone) {
           String line = inStream.readLine();

           String result = handleMessage(line);
           System.err.println("Got server message");
           if (!result.equals("QUIT")) {
               outStream.println(result);
               outStream.flush();
           } else {
               inStream.close();
               outStream.close();
               mySocket.close();
               notDone = false;
               System.out.println("Connection Closed");
           }
       }
   } catch (SocketException se) {
       System.err.println("Detected Client Disconnect");
   } catch (IOException e) {
       System.err.println(
           "Error in client handler, could not establish streams or "
            +  " there was a disconnect");
   }
}
/**
* This method takes the string read in from the socket stream
* and decides what to do with it based on the protocol.
*
* @param line the input line to handle
*
* @return a String which is the server response to the message
*/	